import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getPublicKey } from '@/api/auth'
import { setToken, getToken, clearToken } from '@/utils/auth'

export const useAuthStore = defineStore('auth', () => {
  // 公钥
  const publicKey = ref(null)
  const getPublicKeyAction = async () => {
    return publicKey.value
  }
  const initPublicKeyAction = async () => {
    const ref = await getPublicKey()
    publicKey.value = ref
  }

  // token
  const token = ref(null)
  const getTokenAction = async () => {
    token.value = getToken()
    return token.value
  }
  const setTokenAction = async (tempToken) => {
    token.value = tempToken
    setToken(token.value)
  }
  const clearTokenAction = async () => {
    token.value = null
    clearToken()
  }

  return {
    getPublicKeyAction, initPublicKeyAction,
    getTokenAction, setTokenAction, clearTokenAction,
  }
})